import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * The type Main.
 */
public class Main {
    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
        Manage m = new Manage(10, 3);
        // 打印用户数据块位图
        m.printUserBlockDistribution();
    }
}


/**
 * The type Block.
 */
class Block {
    /**
     * The Id.
     */
    public int id;
    /**
     * The Data.
     */
    public String data;

    /**
     * Instantiates a new Block.
     *
     * @param id the id
     */
    public Block(int id){
        this.id = id;
    }

}

/**
 * The type User.
 */
class User {
    /**
     * The Id.
     */
    public int id;
    /**
     * The Block index.
     */
    public int[] blockIndex;

    /**
     * Instantiates a new User.
     *
     * @param id        the id
     * @param block_num the block num
     */
    public User(int id, int block_num){
        this.id = id;
        this.blockIndex = new int[block_num];
        Arrays.fill(this.blockIndex, 0);
    }
}

/**
 * The type Manage.
 */
class Manage {
    /**
     * The Blocks.
     */
    public List<Block> blocks;
    /**
     * The Users.
     */
    public List<User> users;

    /**
     * Instantiates a new Manage.
     *
     * @param block_num the block num
     * @param user_num  the user num
     */
    public Manage(int block_num, int user_num) {
        this.blocks = new ArrayList<>();
        this.users = new ArrayList<>();

        for(int i=0;i<block_num;i++) {
            blocks.add(new Block(i));
        }
        for(int i=0;i<user_num;i++) {
            users.add(new User(i, block_num));
        }

        // 若数据块数能被所有用户均分，则均分所有数据块，并调整首尾位置用户
        if(block_num%user_num==0) {
            int avg = block_num/user_num;
            for(int i=0;i<users.size();i++) {
                User u = users.get(i);
                for(int j=i*avg;j<(i+1)*avg;j++) {
                    u.blockIndex[j] = 1;
                }
            }
            // 调整首尾用户所拥有数据块
            users.get(0).blockIndex[0] = 0;
            users.get(users.size()-1).blockIndex[0] = 1;
        } else {
            int mod = block_num%user_num;
            int avg = (block_num-mod)/user_num;
            for(int i=0;i<users.size();i++) {
                User u = users.get(i);
                for(int j=i*avg;j<(i+1)*avg;j++) {
                    u.blockIndex[j] = 1;
                }
            }
            for(int i=0;i<mod;i++) {
                users.get(users.size()-1).blockIndex[block_num-1-i] = 1;
            }
        }

    }

    /**
     * Print user block distribution.
     */
    public void printUserBlockDistribution() {
        for (int i=0;i<users.size();i++) {
            User u = users.get(i);
            System.out.format("用户%d的数据块位图：%s\n", i, Arrays.toString(u.blockIndex));
        }
    }
}


